<?php
class crafts {

	var $CI;
	var $crafts_model;

	public function __construct() {
		$this->CI =& get_instance();
		$this->CI->load->model("sys/sys_crafts_model");
		$this->crafts_model =$this->CI->sys_crafts_model;
	}

	public function get_craftss($parent_id) {
		return $this->crafts_model->query_craftss($parent_id);
	}

	public function save_crafts($crafts){
		$check_result=$this->check_crafts($crafts);
		if (!$check_result['result']) {
			return $check_result;
		}
		$crafts_id =$this->crafts_model->insert_crafts($crafts);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/sys_relate_manage/crafts_list";
		return $result;
	}

	public function check_crafts($crafts,$crafts_id=""){
		$result['result'] =true;
		$result['message']="制作工艺名称可以使用";
		if ($crafts['crafts_name']=="") {
			$result['result']=false;
			$result['message']="制作工艺名称不能为空";
			return $result;
		}
		$crafts_info =$this->get_crafts_info_by_name($crafts['crafts_name']);
		if ($crafts_info=="") {
			$result['result']=true;
			$result['message']="制作工艺名称可以使用";
			return $result;
		}
		if ($crafts_info['crafts_id']!=$crafts_id) {
			$result['result']=false;
			$result['message']="制作工艺已经存在,请重新输入";
			return $result;
		}
		return $result;
	}


	public function query_crafts_info($crafts_id){
		return $this->crafts_model->query_crafts_info($crafts_id);
	}

	public function get_crafts_info_by_name($crafts_name){
		return $this->crafts_model->query_crafts_info_by_name($crafts_name);
	}

	public function set_delete_crafts($crafts_id){
		$this->crafts_model->set_delete_crafts($crafts_id);

	}
	public function update_crafts($crafts,$crafts_id){
		$check_result=$this->check_crafts($crafts,$crafts_id);
		if (!$check_result['result']) {
			return $check_result;
		}
		$crafts_id =$this->crafts_model->update_crafts($crafts,$crafts_id);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/sys_relate_manage/crafts_list";
		return $result;
	}

	public function query_crafts_list(){
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->crafts_model->query_crafts_list($start,$limit);
	}
	public function query_crafts_num(){
		return $this->crafts_model->query_crafts_num();
	}
	public function get_all_crafts(){
		return $this->crafts_model->get_all_crafts();
	}
}
?>